package edu.mama.college.mapper;

import edu.mama.college.model.UserCourse;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository(value ="UserCourseMapper")
public interface UserCourseMapper {

    List<UserCourse> userCourseList(@Param("userId") int userId,@Param("currentIndex") int currentIndex,@Param("size") int size);

    @Select("SELECT count(uc.id) total FROM user_course uc LEFT JOIN course c on c.id = uc.course_id LEFT JOIN lesson l on l.id = uc.lesson_id where uc.user_id = #{userId}")
    int userCourseTotal(@Param("userId") int userId);

    @Select("select * from user_course where user_id = #{userId} and course_id = #{courseId}")
    UserCourse userCourse(@Param("userId") int userId,@Param("courseId") int CourseId);

    @Select("INSERT INTO user_course (user_id,course_id,lesson_id) VALUES(#{userId},#{courseId},#{lessonId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addUserCourse(UserCourse usercourse);

    @Select("UPDATE user_course set studied_mins = studied_mins + 1 ,lesson_id = #{lessonId} where user_id = #{userId} and course_id = #{courseId}")
    void updateStudying(UserCourse usercourse);

    @Select("UPDATE user_course set `status` = 'EXPIRED' where (TIMESTAMPDIFF(YEAR,join_time,CURRENT_TIME) >= 1 and `status` = 'ACTIVE')")
    void updateStatus();


}
